#include <stdio.h>
#include <stdlib.h>
#include "dominion.h"
#include "rngs.h"
#include "math.h"
#include <assert.h>
#include <string.h>

void prepRandom(int strm, int seed){
  SelectStream(strm);
  PutSeed((long)seed);
}

int getRandomNumber(int min, int max){
	int number = floor(Random() * (max-min));
	number += min;
	return number;

}

void randomizeSupply(struct gameState *g){
	int i;
	for(i=0; i < 27; i++){
		g->supplyCount[i] = getRandomNumber(0, 40);
	}
}

void randomizeHands(struct gameState *g){
	int players = g->numPlayers;
	int i, j;
	for(i=0; i < players; i++){
		g->handCount[i] = getRandomNumber(0, MAX_HAND);
		for(j = 0; j < g->handCount[i]; j++){
			g->hand[i][j] = getRandomNumber(0, 26);
		}

	}
}

void randomizeDecks(struct gameState *g){
	int players = g->numPlayers;
	int i, j;
	for(i=0; i < players; i++){
		g->deckCount[i] = getRandomNumber(0, MAX_DECK);
		for(j = 0; j < g->deckCount[i]; j++){
			g->deck[i][j] = getRandomNumber(0, 26);
		}

	}
	
}

void randomizeDiscard(struct gameState *g){
	int players = g->numPlayers;
	int i, j;
	for(i=0; i < players; i++){
		g->discardCount[i] = getRandomNumber(0, MAX_DECK);
		for(j = 0; j < g->discardCount[i]; j++){
			g->discard[i][j] = getRandomNumber(0, 26);
		}

	}
	
}
void randomizeTurn(struct gameState *g){
	g->whoseTurn = getRandomNumber(0, g->numPlayers);
}

int makeRandomGameState(struct gameState *g){
//	printf("Randomizing Game\n");
	g->numPlayers = getRandomNumber(2, 4);
	randomizeSupply(g);
	randomizeDecks(g);
	randomizeDiscard(g);
	randomizeHands(g);
	randomizeTurn(g);
//	g->coins = getRandomNumber(0, 20);
//	printf("Game Randomized\n");
	return 0;
}



int testIsGameOver(struct gameState *g){
	int test = isGameOver(g);
	int testing;
	int i, j = 0;
	for(i=0; i< (treasure_map +1); i++){
		if(g->supplyCount[i] == 0){
			if(i == province){
				j = 4;
			}
			else{
				j ++;
			}
		}
	}
	if(g->numPlayers == 4){
		if(j >=4){
			testing = 1;
		}
		else testing = 0;
	}
	else{
		if(j >= 3){
			testing = 1;
		}
		else
			testing = 0;
	}
	assert(testing == test);
		//printf("Test not the same!  My test: %d, it's test: %d numPlayers: %d\n", testing, test, g->numPlayers);
		//return 0;
	
	return 1;
}

int main(int argc, char** argv){
	int strm = 1, seed = 4, min = 2, max = 50;
	if(argc > 1){
		strm = atoi(argv[1]);
	}
	if(argc > 2){
		seed = atoi(argv[2]);
	}
	if(argc > 3){
		min = atoi(argv[3]);
	}
	if(argc > 4){
		max = atoi(argv[4]);
	}
	prepRandom(strm, seed);
	int i, j, count;
	printf("Testing isGameOver()");
	struct gameState *g = newGame();
	for(i=0; i < 1000000; i++){
		makeRandomGameState(g);
		testIsGameOver(g);
	/*	if(testIsGameOver(g) == 0){
			count = 0;
			for(j = 0; j < (treasure_map + 1); j++){
				if (g->supplyCount[j] == 0){
					count ++;
			//		printf("supply [%d] = %d\n", j, g->supplyCount[j]);
				}
			}
			printf("j count: %d\n", count);
		}*/
	}
	printf("\nTest Complete");
	return 0;
}
